<?php
/**
 * Controller_Product
 *
 * Класс Controller_Product отвечает за обработку страниц товаров
 *
 * @author Mike
 */
class Controller_Product extends Controller_Base {

	/**
	 * Выбор действия по URL
	 *
	 */
	public function action() {
		$this->addCrumb('Главная', W_ABSPATH);
		switch ($this->arrRequest[1]) {
			case '':
				$this->actionProductsList();
				break;
			default:
				if($this->arrRequest[0] == 'properties') {
					$url = Service_Escape::absolutelyEscapeString($this->arrRequest[1]);
					$objProductPropertyValue = new ProductPropertyValue();
					$objProductPropertyValue->find('first', array('conditions' => "`product_property_value`.`url`='" . $url . "'"));
					if(!$objProductPropertyValue->id) throw new Exception('', 404);
					$this->actionProductsByProperty($objProductPropertyValue);
					break;
				}
				
				$productId = intval($this->arrRequest[1]);
				$objProduct = new Product();
				$objProduct->find('first', array(
						'select' => '`product`.*, `category`.`category_name`, `category`.`url` `category_url`',
						'conditions' => "`product`.`ID_product` = '" . $productId . "'", 
						'joins' => 'LEFT JOIN `category` USING(`ID_category`)'));
				if(!$objProduct->id) throw new Exception('', 404);
				$this->actionProductPage($objProduct);
		}
	}

	/**
	 * Страница товара
	 *
	 * @param Product $objProduct - объект продукта
	 */
	public function actionProductPage(Product $objProduct) {
		$arrCrumbs = Category::getCategoryCrumbs($objProduct->getField('ID_category'));
		$crumbUrl = W_ABSPATH . 'categories/';
		$this->arrActiveCategoriesIds = array();
		foreach($arrCrumbs as $arrCrumb) {
			$crumbUrl .= $arrCrumb['url'];
			$this->addCrumb($arrCrumb['category_name'], $crumbUrl);
			$this->arrActiveCategoriesIds[] = $arrCrumb['ID_category'];
		}
		$this->addCrumb($objProduct->getField('product_name'), Product::getProductUrl($objProduct->tableFields));

		$arrPhotos = $objProduct->getPhotosIds();					// Айдишники фотографий товара
		
		$this->bodyAttr = 'product';
		$this->keywords = $objProduct->getField('meta_keywords');
		$this->description = $objProduct->getField('meta_description') ? $objProduct->getField('meta_description') : Service_TextMaker::getProductDescription($objProduct);
		$this->title = 'Купить ' . $objProduct->getField('product_name') . ', отзывы, цены и фотографии товара';
		$this->pageHead = $objProduct->getField('product_name') . (User_Seance::isAdmin() ? ' ' . '<a href="' . W_ADMIN . 'products/edit/' . $objProduct->id . '">(Редактировать)</a>' : '');
				
		$this->showHeader();
			View_Product::showProductPage($objProduct, $arrPhotos);
		$this->showFooter();
	}
	
	/**
	 * Список всех товаров
	 *
	 */
	public function actionProductsList() {
		$this->showHeader();
		echo 'Изюм';
		$this->showFooter();
	}
	
	/**
	 * Список всех товаров, имеющих в качестве свойства значение с переданным id
	 *
	 * @param ProductPropertyValue $objProductPropertyValue - объект значения свойства товаров
	 */
	public function actionProductsByProperty(ProductPropertyValue $objProductPropertyValue) {
		$this->addCrumb($objProductPropertyValue->getField('value_page_name'), W_ABSPATH . 'properties' . '/' . $objProductPropertyValue->getField('url'));
		$objProduct = new Product();
		$arrProducts = $objProduct->find('all', array(
			'joins' => 'INNER JOIN `product_vs_property_value` USING(`ID_product`)',
			'conditions' =>  "`product_vs_property_value`.`ID_product_property_value`='" . $objProductPropertyValue->id . "'",
			'order' => '`product`.`created_at` DESC'
		));
		
		$this->bodyAttr = 'category';
		$this->description = $objProductPropertyValue->getField('meta_description');
		$this->keywords = $objProductPropertyValue->getField('meta_keywords');
		$this->title = $objProductPropertyValue->getField('value_page_name');
		$this->pageHead = $this->title . (User_Seance::isAdmin() ? ' ' . '<a href="' . W_ADMIN . 'product_property_values/edit/' . $objProductPropertyValue->id . '">(Редактировать)</a>' : '');
		
		$this->showHeader();
			View_ProductProperty::showPropertyValuePage($objProductPropertyValue, $arrProducts);
		$this->showFooter();
	}
}